 <?php
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
Zend_Loader::loadClass('Zend_Gdata_Extension_When');
Zend_Loader::loadClass('Zend_Gdata_Extension_Who');
Zend_Loader::loadClass('Zend_Gdata_Extension_Reminder');

/*
    EXAMPLE:
    $event = new GoogleCalendar;
    $event->title = "Simple Test";
    $event->content = "Full description";
    $event->where = "Address";
    $event->start = array(Date => "2009-10-06", Time => "10:00");
    $event->end = array(Date => "2009-10-06", Time => "12:00");
    $event->who = array("invite1@domain.com", "invite2@domain.com");
    
    $event->sendNotification = true;
    $event->sendReminder = true;
    
    $event->create();
    
    print $event."\n";
*/
class GoogleCalendar {

    private $_user = 'careers@eyepax.com';
    private $_pass = 'Ep2685107';
    private $_client;
    private $_newEvent;
    private $_event;
    private $data;
   
    
    function __construct() {
        $service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // predefined service name for calendar
        $this->_client = Zend_Gdata_ClientLogin::getHttpClient($this->_user, $this->_pass, $service);
        
        // default value
        $this->start = date("Y-m-d h:i");
        $this->end = date("Y-m-d h:i");
        $this->reminder = array("minutes" => 60, "method" => "email");
        $this->tzOffset = "+05:30";
    }
    
    function __set ($key, $value) {
        $this->data[$key] = $value;
    }

    function __get ($key) {
        return $this->data[$key];
    }

    function __toString () {
        return $this->_event->id->text;
    }

    function create () {

        $gdataCal = new Zend_Gdata_Calendar($this->_client);
        $this->_newEvent = $gdataCal->newEventEntry();

        $this->_newEvent->title = $gdataCal->newTitle($this->title);
        $this->_newEvent->where = array($gdataCal->newWhere($this->where));
        $this->_newEvent->content = $gdataCal->newContent($this->content);

        $when = $gdataCal->newWhen();
        $when->startTime = $this->start["Date"]."T".$this->start["Time"].":00.000".$this->tzOffset;
        $when->endTime   = $this->end["Date"]."T".$this->end["Time"].":00.000".$this->tzOffset;
        
        if ($this->sendReminder) {
            $reminder = $gdataCal->newReminder();
            $reminder->setMinutes($this->reminder['minutes']);
            $reminder->setMethod($this->reminder['method']);
            $when->setReminders(array($reminder));
        }
        $this->_newEvent->when  = array($when);

        if (is_array($this->who)) {
            while (list(,$mail) = each($this->data["who"])) {
                $who = $gdataCal->newWho(); 
                $who->setEmail($mail); 
                $status = new Zend_Gdata_Extension_AttendeeStatus();
                $status->setValue("http://schemas.google.com/g/2005#event.accepted");
                $who->setAttendeeStatus($status);
                $whoArray[] = $who;
            }
            if (is_array($whoArray)) { $this->_newEvent->setWho($whoArray); }
        }
        
        if ($this->sendNotification) {
            $sendEventNotifications = new Zend_Gdata_Calendar_Extension_SendEventNotifications();
            $sendEventNotifications->setValue( true );
            $this->_newEvent->setSendEventNotifications( $sendEventNotifications );
        }
      //  $this->_event = $gdataCal->insertEvent($this->_newEvent,'https://www.google.com/calendar/feeds/'.$calid.'/private/full');
        $this->_event = $gdataCal->insertEvent($this->_newEvent,null);
         
        return $this->_event;
    }
}

?> 